<?php


/**
 * 分销
 */
class Api_Agent extends PhalApi_Api
{
	
	public function getRules()
	{
		return array(
			'getCode' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
			),
			
			'checkAgent' => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
			),
			'quit'       => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
			),
			'setAgent'   => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'code'  => array('name' => 'code', 'type' => 'string', 'desc' => '用户token'),
			),
			'profit'     => array(
				'uid'   => array('name' => 'uid', 'type' => 'int', 'desc' => '用户ID'),
				'token' => array('name' => 'token', 'type' => 'string', 'desc' => '用户token'),
				'p'     => array('name' => 'p', 'type' => 'int', 'default' => '1', 'desc' => '页面'),
				'pnum'  => array('name' => 'pnum', 'type' => 'int', 'default' => '20', 'desc' => '每一页数量'),
			),
		);
	}
	
	
	/**
	 * 分享信息
	 * @desc 用于 获取分享信息
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return string info[0].code 邀请码
	 * @return string info[0].href 二维码链接
	 * @return string info[0].qr 二维码图片链接
	 * @return string msg 提示信息
	 */
	public function getCode()
	{
		// 参数
		$uid        = checkNull($this->uid);
		$token      = checkNull($this->token);
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		}
		
		// 信息
		$domain = new Domain_Agent();
		$info   = $domain->getCode($uid);
		if(!$info){
			return $this->fail('信息错误！', 1001);
		}
		
		// 二维码等
		$configpri          = getConfigPri();
		$openinstall_switch = $configpri['openinstall_switch'];
		$openinstall_appkey = $configpri['openinstall_appkey'];
		if($openinstall_switch && $openinstall_appkey != ""){
			$href = get_upload_path("/appapi/agent/downapp?code=".$info['code']);
			$href = get_upload_path("/?code=".$info['code']);
			$qr   = scerweima($href, 1, $uid);
		} else{
			$href = get_upload_path('/portal/index/scanqr');
			$href = get_upload_path('/');
			$qr   = scerweima($href);
		}
		
		// 整理
		$info['agent']  = $domain->agent($uid);  // 上级
		$info['profit'] = $domain->profit($uid); // 我的收益
		$info['href']   = $href;
		$info['qr']     = get_upload_path($qr);
		
		return $this->success([0 => $info]);
	}
	
	
	/**
	 * 获取邀请开关、邀请码必填开关、openinstall开关以及用户是否设置了邀请码
	 * @desc 用于获取邀请开关、邀请码必填开关、openinstall开关以及用户是否设置了邀请码
	 * @return int code 操作码，0表示成功
	 * @return array info
	 * @return int info[0]. agent_switch 邀请开关 1打开 0关闭
	 * @return int info[0]. agent_must 邀请码是否必填 1是 0否
	 * @return int info[0]. has_agent 是否已经设置过邀请码 1是 0否
	 * @return int info[0]. openinstall_switch openinstall开关 1开 0关
	 * @return string msg 提示信息
	 */
	public function checkAgent()
	{
		$uid        = checkNull($this->uid);
		$token      = checkNull($this->token);
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		} elseif($checkToken == 10020){
			return $this->fail('该账号已被禁用！', 700);
		}
		
		$configpri = getConfigPri();
		// 整理
		$info = array(
			'agent_switch'       => $configpri['agent_switch'],
			'agent_must'         => $configpri['agent_must'],
			'has_agent'          => (string)checkAgentIsExist($uid),
			'openinstall_switch' => $configpri['openinstall_switch'],
		);
		
		return $this->success([0 => $info]);
	}
	
	
	// 设置我的上级代理商
	public function setAgent()
	{
		// 参数
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$code  = checkNull($this->code);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		}
		if($code == ""){
			return $this->fail('邀请码不能为空！', 1001);
		}
		
		// 信息
		$domain = new Domain_Agent();
		$info   = $domain->setAgent($uid, $code);
		if($info == 1001){
			return $this->fail('你已经设置！', $info);
		} elseif($info == 1002){
			return $this->fail('邀请码错误！', $info);
		} elseif($info == 1003){
			return $this->fail('不能填写自己的邀请码！', $info);
		} elseif($info == 1004){
			return $this->fail('您已经是该用户的上级！', $info);
		}
		
		return $this->success();
	}
	
	
	// 退出代理
	public function quit()
	{
		// 参数
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		}
		
		// 信息
		$domain = new Domain_Agent();
		$info   = $domain->quit($uid);
		
		return $this->success();
	}
	
	// 代理收入
	function profit()
	{
		// 参数
		$uid   = checkNull($this->uid);
		$token = checkNull($this->token);
		$p     = checkNull($this->p);
		$pnum  = checkNull($this->pnum);
		if(!$p){
			$p = 1;
		}
		if($pnum < 1){
			$pnum = 20;
		}
		
		$checkToken = checkToken($uid, $token);
		if($checkToken == 700){
			return $this->fail('您的登陆状态失效，请重新登陆！', 700);
		}
		
		// 信息
		$domain = new Domain_Agent();
		$lists  = $domain->getProfit($uid, $p, $pnum);
		$total  = $domain->sumProfit($uid);
		$result = ['list' => $lists, 'total' => $total];
		
		
		return $this->success($result);
	}
	
	
	//扫描app生成的分享二维码显示的下载页面，通过codeinstall 自动建立上下级关系
	public function downapp()
	{
		$data = $this->request->param();
		$code = '';
		if(!isset($data['code'])){
			$this->assign("reason", '邀请码错误');
			
			return $this->fetch(':error');
		}
		
		$code      = $data['code'];
		$code_info = Db::name("agent_code")->where("code='{$code}'")->find();
		if(!$code_info){
			$this->assign("reason", '邀请码不存在');
			
			return $this->fetch(':error');
		}
		
		$configpub          = getConfigPub();
		$site_name          = $configpub['site_name'];
		$configpri          = getConfigPri();
		$openinstall_switch = $configpri['openinstall_switch'];
		if(!$openinstall_switch){
			$this->assign("reason", '分享通道关闭');
			
			return $this->fetch(':error');
		}
		$openinstall_appkey = $configpri['openinstall_appkey'];
		if(!$openinstall_appkey){
			$this->assign("reason", '信息配置错误');
			
			return $this->fetch(':error');
		}
		$this->assign("site_name", $site_name);
		$this->assign("openinstall_appkey", $openinstall_appkey);
		
		return $this->fetch();
	}
	
}

